/*!
    \copyright  Copyright (c) 2023 Qualcomm Technologies International, Ltd.
                All Rights Reserved.
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \file       storage_manager_factory.c
    \brief      The available storage manager implementations as singletons.
*/

#include "storage_manager_interface.h"
#include <logging.h>

extern const storage_mgr_interface_t storage_ra_partition;

static singleton_instance_t singleton_table [] = 
{
    {
        .type = storage_mgr_ra_partition,
        .interface = &storage_ra_partition,
        .is_initialized = FALSE
    }
};

const storage_mgr_interface_t * StorageManager_GetSingletonInterfaceByType(storage_mgr_type_t type)
{
    const storage_mgr_interface_t * manager = NULL;

    for (unsigned i = 0; i < ARRAY_DIM(singleton_table); i++)
    {
        if (singleton_table[i].type == type)
        {
            if (!singleton_table[i].is_initialized)
            {
                singleton_table[i].interface->StorageManager_Initialize();
                singleton_table[i].is_initialized = TRUE;
            }
            manager = singleton_table[i].interface;
            break;
        }
    }
    
    if (!manager)
    {
        DEBUG_LOG_PANIC("%s: Unsupported storage type enum:storage_manager_storage_type_t:%d", __func__, type);
    }

    return manager;
}

const singleton_instance_t * StorageManager_GetSingletonInstanceTable(void)
{
    return singleton_table;
}

size_t StorageManager_GetSingletonInstanceTableSize(void)
{
    return ARRAY_DIM(singleton_table);
}